package com.hardroid.workload.ui.fragment;

import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.IResultReplacable;
import com.hardroid.workload.workcalc.Employer;

public class ResultControlFragment extends Fragment {

	private Button nextButton;
	private Button prevButton;
	private TextView employerText;

	public ArrayList<Employer> lEmployer;
	public int currentEmployerId = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_result_control, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		nextButton = (Button) getView().findViewById(
				R.id.result_control_next_button);
		prevButton = (Button) getView().findViewById(
				R.id.result_control_previous_button);
		employerText = (TextView) getView().findViewById(
				R.id.result_control_employer_text);

		nextButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (currentEmployerId < lEmployer.size() - 1) {
					currentEmployerId++;
					updateView();
				}
			}
		});

		prevButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (currentEmployerId > 0) {
					currentEmployerId--;
					updateView();
				}
			}
		});
		updateView();
	}

	public void setListEmployer(ArrayList<Employer> l) {
		lEmployer = l;
	}

	public void updateView() {
		nextButton.setEnabled(true);
		prevButton.setEnabled(true);
		if (currentEmployerId == 0) {
			prevButton.setEnabled(false);
		}

		if (currentEmployerId == lEmployer.size() - 1) {
			nextButton.setEnabled(false);
		}

		employerText.setText((currentEmployerId +1)+ " "
				+ lEmployer.get(currentEmployerId).name);

		Log.i("ObsvControl", "replace " + currentEmployerId);
		((IResultReplacable) getActivity())
				.replaceResultFragment(currentEmployerId);
	}

	public void setEmployer(ArrayList<Employer> l) {
		this.lEmployer = l;
	}
}
